import datetime
import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_login(self):
        """
         测试登录
        """
        self.browser.get('http://aw.com:81/admin/account/login.html')
        sleep(1)
        self.browser.find_element(By.NAME, 'account').send_keys('admin')
        self.browser.find_element(By.NAME, 'password').send_keys('123')
        self.browser.find_element(By.ID, 'login').click()
        sleep(2)
    def test_query_shop(self):
        """
         测试查询商品
        """
        #点击商品管理
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        #定位商品名称搜索框
        self.browser.find_element(By.ID, 'keyword').send_keys('文具')
        #定位查询按钮点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        #商品列表
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list) == 0:
            print('未查询到商品')
        else:
            for shop in shop_list:
                shop_name = shop.find_element(By.XPATH, './td[2]/div').text
                if '文具' in shop_name:
                    print(shop_name)
    def test_logout(self):
        """
         测试退出登录
        """
        #退出子框架
        self.browser.switch_to.default_content()
        ##定位用户信息
        df = self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        #鼠标悬停
        ActionChains(self.browser).move_to_element(df).perform()
        sleep(2)
        
       #点击退出
        self.browser.find_element(By.XPATH, '//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
        
    def test_add_shop(self):
        """
        测试添加商品
        """
        #点击商品管理
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        #定位添加商品按钮点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        # 定位商品子发布框架
        iframe_add = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #定位商品名称输入框并输入商品名称
        now = datetime.datetime.now().strftime( "%Y-%m-%d %H-%M-%S")
        self.browser.find_element(By.NAME, 'name').clear()
        self.browser.find_element(By.NAME, 'name').send_keys('测试商品'+now)
        sleep(1)
        #定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(2)
        #定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(2)
        #定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(2)
        #定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(2)
        #定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(2)
        #定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(2)
        
        # 定位商品主图片的添加图片按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        #定位图片上传子框架并进入子框架
        self.test_upload_img('E:\\pro\\1.jpg','//*[@id="layui-layer-iframe2"]')
        sleep(2)
        #返回商品子框架
        self.browser.switch_to.parent_frame()
        
        #切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        #定位商品轮播图的添加图片按钮并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('E:\\pro\\1.jpg','//*[@id="layui-layer-iframe3"]')
        
        #返回商品子框架
        self.browser.switch_to.parent_frame()
        
        #切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 切换到价格设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()
        sleep(2)
        # 定位市场价输入框并输入市场价
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[2]/input').send_keys('100')
        sleep(1)
        #定位价格输入框并输入价格
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input').send_keys('90')
        sleep(1)
        #定位成本输入框并输入成本
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[4]/input').send_keys('80')
        sleep(1)
        #定位库存输入框并输入库存
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[5]/input').send_keys('100')
        sleep(1)
        #切换销售设置标签页
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        sleep(2)
        #定位配送方式的快递发货并点击按钮  
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
        sleep(1)
        #返回到商品发布子框架
        self.browser.switch_to.parent_frame()
        sleep(1)
        #点击保存按钮
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
        
    def test_sj_shop(self):
        #测试上架商品
        #点击商品管理
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite').click()
        sleep(2)
        #定位iframe的子框架
        iframe = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        #定位仓库中商品切换标签页
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        #接收商品列表
        good_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(good_list) > 0:
            #定位第一个商品的上架按钮并点击
            self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[2]').click()
            sleep(2)
            #定位确认上架按钮并点击
            self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
        
    def test_upload_img(self,im_path, xpath_path):
        """
        测试上传商品图片
        """
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 定位图片上传子框架并进入子框架
        iframe_img = self.browser.find_element(By.XPATH, xpath_path)
        self.browser.switch_to.frame(iframe_img)
        sleep(2)
        # 定位图片上传输入框并上传图片
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[1]/div[1]/input').send_keys(im_path)
        sleep(4)
        # 选择第一张图片
        self.browser.find_element(By.XPATH, '/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(2)
        #点击使用图片按钮   
        self.browser.find_element(By.XPATH, '//*[@id="used"]').click()
        sleep(2)
        
    def test_edit_shop(self):
        """
        测试编辑商品
        """
        #点击商品管理
        self.browser.find_element(By.XPATH, '//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        #定位iframe子框架
        iframe = self.browser.find_element(By.XPATH, '//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        #定位编辑商品按钮点击
        self.browser.find_element(By.XPATH, '/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
        sleep(2)
        # 定位商品子发布框架
        iframe_edit = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_edit)
        sleep(2)
        #定位商品编辑框并编辑商品名称
        now = datetime.datetime.now().strftime( "%Y-%m-%d %H-%M-%S")
        self.browser.find_element(By.NAME, 'name').clear()
        self.browser.find_element(By.NAME, 'name').send_keys('商品'+now)
        sleep(1)
        #修改定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(2)
        #定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(2)
        #定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(2)
        #定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[3]').click()
        sleep(2)
        #定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(2)
        #定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[3]').click()
        sleep(2)
        
        #返回商品子框架
        self.browser.switch_to.parent_frame()
       
        # 保存修改
         #点击保存按钮
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()
        sleep(2)
           

if __name__ == '__main__':
    test = LikeShopTest()
    test.test_login()
    # test.test_query_shop()
    # test.test_logout()
    # test.test_add_shop()
    test.test_edit_shop()
    # test.test_sj_shop()